package com.tbs.parkingsaleapp.UI.Adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.tbs.parkingsaleapp.Http.AsycHttp;
import com.tbs.parkingsaleapp.Http.JsonData.Message;
import com.tbs.parkingsaleapp.Http.Listenner.MessageReplyer;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.UI.ParkDetailActivity;

import java.util.ArrayList;

public class MsgAdapter extends BaseAdapter {

    ListView lv;
    ArrayList<Message> arrayList=new ArrayList<>();
    public MsgAdapter(Message[] msg,ListView lv)
    {
        this.lv=lv;
        for (Message s:msg
             ) {
            if(s.getHandled()==0)
                arrayList.add(s);
        }
    }
    public void UpdateList(Message msg)
    {
        arrayList.remove(msg);
        lv.setAdapter(this);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return arrayList.get(position).getId();
    }
    View TextMessage(Message msg, Context context)
    {
        View root=View.inflate(context, R.layout.text_msg,null);
        TextView textView=root.findViewById(R.id.msg_text);
        textView.setText(String.format("%s:\n%s\n发送时间:%s",msg.getFrom(),msg.getContent(),msg.getTime()));
        ImageButton button1=root.findViewById(R.id.msg_read);
        View finalRoot = root;
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageReplyer replyer=new MessageReplyer(new MessageReplyer.IHandleReuslt() {
                    @Override
                    public void Action(int r, int k) {
                        if(r>0)
                        {
                            Toast.makeText(finalRoot.getContext(), "已读标记成功", Toast.LENGTH_SHORT).show();
                            UpdateList(msg);

                        }
                        else
                            Toast.makeText(finalRoot.getContext(), "操作失败", Toast.LENGTH_SHORT).show();
                    }
                });
                AsycHttp<Object,Integer> http=new AsycHttp<Object,Integer>().setContext(finalRoot.getContext()).setBackgroundAction(replyer).setResultAction(replyer);
                http.execute(msg.getId(),null);
            }
        });
        return root;
    }
    View DiscountPicker(Message msg,Context context)
    {
        View root=View.inflate(context, R.layout.pick_discount_msg,null);
        TextView textView1= root.findViewById(R.id.msg_discount_text);
        textView1.setText(String.format("%s:\n%s\n发送时间:%s",msg.getFrom(),msg.getContent(),msg.getTime()));
        Button button=root.findViewById(R.id.msg_discount_pick);
        View finalRoot1 = root;
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageReplyer replyer=new MessageReplyer(new MessageReplyer.IHandleReuslt() {
                    @Override
                    public void Action(int r, int k) {
                        if(r>0)
                        {
                            Toast.makeText(context, "领取成功", Toast.LENGTH_SHORT).show();
                            UpdateList(msg);
                        }

                        else
                            Toast.makeText(context, "领取失败", Toast.LENGTH_SHORT).show();
                    }
                });
                AsycHttp<Object,Integer> http=new AsycHttp<Object,Integer>().setContext(context).setBackgroundAction(replyer).setResultAction(replyer);
                http.execute(msg.getId(),1);
            }
        });
        Button button2=root.findViewById(R.id.msg_discount_refuse);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageReplyer replyer=new MessageReplyer(new MessageReplyer.IHandleReuslt() {
                    @Override
                    public void Action(int r, int k) {
                        if(r>0)
                        {
                            Toast.makeText(context, "拒绝成功", Toast.LENGTH_SHORT).show();
                            UpdateList(msg);
                        }

                        else
                            Toast.makeText(context, "操作失败", Toast.LENGTH_SHORT).show();
                    }
                });
                AsycHttp<Object,Integer> http=new AsycHttp<Object,Integer>().setContext(context).setBackgroundAction(replyer).setResultAction(replyer);
                http.execute(msg.getId(),0);
            }
        });
        return root;
    }
    View Jump(Message msg,Context context)
    {
        View root=View.inflate(context, R.layout.jump_msg,null);
        TextView textView1= root.findViewById(R.id.msg_jump_text);
        textView1.setText(String.format("%s:\n%s\n发送时间:%s",msg.getFrom(),msg.getContent(),msg.getTime()));
        Button button=root.findViewById(R.id.msg_jump);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer[] arr= JSON.parseObject(msg.getArg(),Integer[].class);
                ParkDetailActivity.ActivityStart(context,arr[0],arr[1]);
            }
        });
        Button button2=root.findViewById(R.id.msg_jump_refuse);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageReplyer replyer=new MessageReplyer(new MessageReplyer.IHandleReuslt() {
                    @Override
                    public void Action(int r, int k) {
                        if(r>0)
                        {
                            Toast.makeText(context, "此条推荐被拒收", Toast.LENGTH_SHORT).show();
                            UpdateList(msg);
                        }

                        else
                            Toast.makeText(context, "操作失败", Toast.LENGTH_SHORT).show();
                    }
                });
                AsycHttp<Object,Integer> http=new AsycHttp<Object,Integer>().setContext(context).setBackgroundAction(replyer).setResultAction(replyer);
                http.execute(msg.getId(),1);
            }
        });
        return root;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View root=null;
        Message msg=arrayList.get(position);

        switch (msg.getType())
        {
            case "Text":
                return TextMessage(msg,parent.getContext());
            case "DiscountPicker":
                return DiscountPicker(msg,parent.getContext());
            case "Jump":
                return Jump(msg,parent.getContext());

        }

        return root;
    }
}
